package com.yjf.ljr.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

public class RulerView extends View {

	private RectF mRectF = new RectF();
	private RectF mRectOutSideLine = new RectF();
	private Paint mPaintLine = new Paint();
	private Paint mPaintBg = new Paint();
	private Paint mPaintOutLine = new Paint();

	private int mRealLen = 0;
	private String mTextShow = "";
	private int mTextSize = 35;
	private int mTextColor = Color.parseColor("#f15928");
	
	private int leftMargin = 10;
	private int rightMargin = 10;
	private int mMax = 25;
	private float rx = 6;
	private float ry = 20;
	private float lineHeight = 18;// 长方形高度

	public RulerView(Context context) {
		super(context);
	}

	public RulerView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public RulerView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		mPaintLine.setColor(Color.LTGRAY);
		mPaintLine.setStyle(Style.FILL);
		mPaintLine.setAntiAlias(true);

		// 绘制外框
		mPaintOutLine.setColor(Color.LTGRAY);
		mPaintOutLine.setStyle(Style.STROKE);
		mPaintOutLine.setAntiAlias(true);
		mRectOutSideLine.left = 0;
		mRectOutSideLine.top = 0;
		mRectOutSideLine.right = super.getWidth();
		mRectOutSideLine.bottom = super.getHeight();
		canvas.drawRoundRect(mRectOutSideLine, 10, 10, mPaintOutLine);// 第二个参数是x半径，第三个参数是y半径
		mPaintOutLine.setStyle(Style.FILL);
		mPaintOutLine.setColor(Color.parseColor("#fafafa"));
		mRectOutSideLine.left = 1;
		mRectOutSideLine.top = 1;
		mRectOutSideLine.right = super.getWidth() - 1;
		mRectOutSideLine.bottom = super.getHeight() - 1;
		canvas.drawRoundRect(mRectOutSideLine, 10, 10, mPaintOutLine);// 第二个参数是x半径，第三个参数是y半径

		// 设置个新的长方形
		mRectF.left = leftMargin;
		mRectF.top = super.getHeight() * 1 / 2;
		mRectF.right = super.getWidth() - rightMargin;
		mRectF.bottom = super.getHeight() * 1 / 2 + lineHeight;
		canvas.drawRoundRect(mRectF, rx, ry, mPaintLine);// 第二个参数是x半径，第三个参数是y半径
		// 画上面的线
		mPaintLine.setColor(Color.parseColor("#aaaaaa"));
		mPaintLine.setStrokeWidth(3);
		// canvas.drawLine(rx + leftMargin, super.getHeight()*1/2,
		// super.getWidth() - rx - rightMargin, super.getHeight()*1/2,
		// mPaintLine);

		// 画刻度
		int size = mMax / 5;
		int rectTop = 12;
		int fiveLen = (super.getWidth() - leftMargin - rightMargin) / size;
		for (int i = 0; i <= size; i++) {
			String text = "" + 5 * i;
			float textX = i * fiveLen - mPaintLine.measureText(text) / 2;
			if (i != 0 && i != size) {
				if (5 * i < mRealLen) {
					canvas.drawLine(i * fiveLen + leftMargin, super.getHeight() * 1 / 2 + lineHeight / 2 - rectTop, i
							* fiveLen + leftMargin, super.getHeight() * 1 / 2 + lineHeight / 2 + rectTop, mPaintLine);
				} else {
					canvas.drawLine(i * fiveLen + leftMargin, super.getHeight() * 1 / 2, i * fiveLen + leftMargin,
							super.getHeight() * 1 / 2 + lineHeight - 1, mPaintLine);
				}
			}
			if (i == 0) {
				textX += mPaintLine.measureText(text) / 2;
			} else if (i == size) {
				textX -= mPaintLine.measureText(text) / 2;
			}
			canvas.drawText(text, textX + leftMargin, (float) super.getHeight() * 1 / 2 + lineHeight / 2 + 30,
					mPaintLine);
		}

		// 画上面的刻度值和背景
		mPaintBg.setColor(Color.parseColor("#6bbae3"));
		mPaintBg.setStyle(Style.FILL);
		mPaintBg.setAntiAlias(true);
		mRectF.left = mRealLen * fiveLen / 5 - 8;
		mRectF.top = super.getHeight() * 1 / 2 - 20 - 15;
		mRectF.right = mRealLen * fiveLen / 5 + mPaintLine.measureText("" + mRealLen) + 12;
		mRectF.bottom = super.getHeight() * 1 / 2 - 20 + 5;
		canvas.drawRoundRect(mRectF, 2, 2, mPaintBg);// 第二个参数是x半径，第三个参数是y半径
		mPaintLine.setColor(Color.WHITE);
		canvas.drawText("" + mRealLen, mRealLen * fiveLen / 5 + 2, (float) super.getHeight() * 1 / 2 - 20, mPaintLine);

		// 画颜色标尺
		mPaintLine.setColor(Color.parseColor("#aa6bbae3"));
		mRectF.left = leftMargin + 3;
		mRectF.top = super.getHeight() * 1 / 2 + lineHeight / 2 - rectTop;
		mRectF.right = leftMargin + fiveLen * mRealLen / 5;
		mRectF.bottom = super.getHeight() * 1 / 2 + lineHeight / 2 + rectTop;
		canvas.drawRect(mRectF, mPaintLine);// 第二个参数是x半径，第三个参数是y半径

		// 画最上面的字
		mPaintLine.setColor(mTextColor);
		mPaintLine.setTextSize(mTextSize);
		canvas.drawText(mTextShow, leftMargin*2, 50, mPaintLine);

		// 画黑色的刻度
		mPaintLine.setColor(Color.parseColor("#aa5f5f5f"));
		mPaintLine.setTextSize(10);
		for (int i = 0; i < 8; i++) {
			canvas.drawLine(leftMargin + i, super.getHeight() * 1 / 2 + lineHeight / 2 - rectTop - 3, i + leftMargin,
					super.getHeight() * 1 / 2 + lineHeight / 2 + rectTop - 10 + i*2, mPaintLine);
			canvas.drawLine(fiveLen * mRealLen / 5 + leftMargin + i - 4, super.getHeight() * 1 / 2 + lineHeight / 2
					- rectTop - 3, fiveLen * mRealLen / 5 + i + leftMargin - 4, super.getHeight() * 1 / 2 + lineHeight
					/ 2 + rectTop - 10 + (8 - i) * 2, mPaintLine);
		}
	}
	
	
	public int getFontHeight(float fontSize) {
		Paint paint = new Paint();
		paint.setTextSize(fontSize);
		FontMetrics fm = paint.getFontMetrics();
		return (int) Math.ceil(fm.descent - fm.top) + 2;
	}

	public int getRealLen() {
		return mRealLen;
	}

	public void setRealLen(int mRealLen) {
		this.mRealLen = mRealLen;
	}

	public String getTextShow() {
		return mTextShow;
	}

	public void setTextShow(String mTextShow) {
		this.mTextShow = mTextShow;
	}

	public int getMax() {
		return mMax;
	}

	public void setMax(int mMax) {
		this.mMax = mMax;
	}

	public int getTextSize() {
		return mTextSize;
	}

	public void setTextSize(int mTextSize) {
		this.mTextSize = mTextSize;
	}

	public int getTextColor() {
		return mTextColor;
	}

	public void setTextColor(int mTextColor) {
		this.mTextColor = mTextColor;
	}
	
}
